package com.ruoyi.dispatch.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.utils.BeanCopyUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dispatch.mapper.DrivingMonthPlanMapper;
import com.ruoyi.dispatch.domain.DrivingMonthPlan;
import com.ruoyi.dispatch.service.IDrivingMonthPlanService;

/**
 * 掘进月计划Service业务层处理
 * 
 * @author wuxiaofang
 * @date 2025-05-19
 */
@Service
public class DrivingMonthPlanServiceImpl implements IDrivingMonthPlanService 
{
    @Autowired
    private DrivingMonthPlanMapper drivingMonthPlanMapper;

    /**
     * 查询掘进月计划
     * 
     * @param drivingMonthPlanId 掘进月计划主键
     * @return 掘进月计划
     */
    @Override
    public DrivingMonthPlan selectDrivingMonthPlanByDrivingMonthPlanId(Long drivingMonthPlanId)
    {
        return drivingMonthPlanMapper.selectDrivingMonthPlanByDrivingMonthPlanId(drivingMonthPlanId);
    }

    /**
     * 查询掘进月计划列表
     * 
     * @param drivingMonthPlan 掘进月计划
     * @return 掘进月计划
     */
    @Override
    public List<DrivingMonthPlan> selectDrivingMonthPlanList(DrivingMonthPlan drivingMonthPlan)
    {
        return drivingMonthPlanMapper.selectDrivingMonthPlanList(drivingMonthPlan);
    }

    /**
     * 新增掘进月计划
     * 
     * @param drivingMonthPlan 掘进月计划
     * @return 结果
     */
    @Override
    public int insertDrivingMonthPlan(DrivingMonthPlan drivingMonthPlan)
    {
        drivingMonthPlan.setCreateTime(DateUtils.getNowDate());
        return drivingMonthPlanMapper.insertDrivingMonthPlan(drivingMonthPlan);
    }

    /**
     * 修改掘进月计划
     * 
     * @param drivingMonthPlan 掘进月计划
     * @return 结果
     */
    @Override
    public int updateDrivingMonthPlan(DrivingMonthPlan drivingMonthPlan)
    {
        drivingMonthPlan.setUpdateTime(DateUtils.getNowDate());
        return drivingMonthPlanMapper.updateDrivingMonthPlan(drivingMonthPlan);
    }

    /**
     * 批量删除掘进月计划
     * 
     * @param drivingMonthPlanIds 需要删除的掘进月计划主键
     * @return 结果
     */
    @Override
    public int deleteDrivingMonthPlanByDrivingMonthPlanIds(Long[] drivingMonthPlanIds)
    {
        return drivingMonthPlanMapper.deleteDrivingMonthPlanByDrivingMonthPlanIds(drivingMonthPlanIds);
    }

    /**
     * 删除掘进月计划信息
     * 
     * @param drivingMonthPlanId 掘进月计划主键
     * @return 结果
     */
    @Override
    public int deleteDrivingMonthPlanByDrivingMonthPlanId(Long drivingMonthPlanId)
    {
        return drivingMonthPlanMapper.deleteDrivingMonthPlanByDrivingMonthPlanId(drivingMonthPlanId);
    }

    @Override
    public List<DrivingMonthPlan> copyLastMonthPlan(String targetMonth) {
        String lastMonth = DateUtils.getlastMonth(targetMonth);

        //查询上月计划数据
        DrivingMonthPlan param = new DrivingMonthPlan();
        param.setMonthDate(lastMonth);
        List<DrivingMonthPlan> lastMonthPlanList = drivingMonthPlanMapper.selectDrivingMonthPlanList(param);

        List<DrivingMonthPlan> targetMonthPlanList = Lists.newArrayList();
        for (DrivingMonthPlan item : lastMonthPlanList) {
            DrivingMonthPlan newPlan = BeanCopyUtils.copyProperties(item,DrivingMonthPlan.class);
            //修改必要字段
            newPlan.setDrivingMonthPlanId(null);
            newPlan.setMonthDate(targetMonth);
            newPlan.setCreateTime(DateUtils.getNowDate());
            newPlan.setUpdateTime(null);
            targetMonthPlanList.add(newPlan);
            //数据入库
            drivingMonthPlanMapper.insertDrivingMonthPlan(newPlan);
        }
        return targetMonthPlanList;
    }
}
